﻿
using Microsoft.AspNetCore.Mvc;
using MyAppBox.Controllers;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;
using System;
using System.Collections.Generic;
using System.Data;
using FineUICore;

namespace MyAppBox.Area.system.Controllers
{
    [Area("system")]
    public class system1Controller : SysPageController
    {
        // GET: system/system1
        public IActionResult Index()
        {
            InitData(null);
            return View();
        }


        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <returns></returns>
        public IActionResult sys1Edit(string id, string str40)
        {
            Initsys1EditData(id);
            return View();
        }

        /// <summary>
        /// 选择人员页面
        /// </summary>
        /// <param name="uid">要过滤的人员ID</param>
        /// <param name="sys10id">角色ID 角色 选择时过滤</param>
        /// <returns></returns>
        public IActionResult userselect(string uid, string sys10id)
        {
            MsQuery msq = new MsQuery();
            if (string.IsNullOrEmpty(uid))
            {
                uid = "-1";
            }
            uid += ",1";//admin 强制不可选
            if (!string.IsNullOrEmpty(sys10id))
            {
                try
                {
                    MsQuery msquser = new MsQuery();
                    msquser.strquery += " and 1=1 and SYSTEM_R3_30 ='" + sys10id + "' and SYSTEM_R3_40='1'";
                    List<Mssystem_r3> listms = new BLLsystem_r3().Getsystem_r3MsByQuery(msquser);
                    if (listms.Count > 0)
                    {
                        uid += "," + string.Join(",", listms.ConvertAll(m => m.System_R3_20));
                    }
                }
                catch { }
            }
            msq.strquery = " and System_1_10 not in (" + uid + ")";
            DataTable dt = new DataTable();
            try
            {
                dt = ((DataSet)new BLLsystem_1().Getsystem_1ByQuery(msq).data).Tables[0];
            }
            catch { }
            ViewBag.UserSelectData = dt;
            return View();
        }

        #region Index页面
        /// <summary>
        /// 初始数据
        /// </summary>
        private void InitData(JArray fields)
        {
            MsQuery ms = new MsQuery();
            if (true)
            {
                ms.strquery += " and 1=1 and system_1_20!='admin' ";
            }
            //List<Mssystem_1> listms = new BLLsystem_1().Getsystem_1MsByQuery(ms);
            DataTable dt = ((DataSet)new BLLsystem_1().Getsystem_1ByQuery(ms).data).Tables[0];
            if (FineUICore.UIHelper.IsFineUIAjaxPostBack())
            {
                var Grid1 = FineUICore.UIHelper.Grid("Grid1");
                if (fields != null)
                {
                    Grid1.DataSource(dt, fields);
                }
            }
            else { ViewBag.griddata = dt; }
        }
        #endregion

        /// <summary>
        /// 人员编辑页面
        /// </summary>
        /// <param name="id"></param>
        private void Initsys1EditData(string id)
        {
            ViewBag.str10 = id != null ? id : "0";
            Mssystem_1 ms1 = new Mssystem_1();
            if (id != null)
            {
                ms1 = new BLLsystem_1().Getsystem_1MsByKey(id);
            }
            ViewBag.data = ms1.ToString();
            //部门列表
            ViewBag.str20Data = null;
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult bendel_Click(JArray ids, JArray fields)
        {
            MsQuery msq = new MsQuery();
            msq.strquery = " and System_1_10 in ('" + string.Join("','", ids) + "')";
            MsReturned rt = new BLLsystem_1().Deletesystem_1ByQuery(msq);
            if (Convert.ToInt32(rt.strMsId) >= 0)
            {
                ShowNotify("保存成功");
                InitData(fields);
            }
            return FineUICore.UIHelper.Result();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult EditFormsave(JObject ms, string type)
        {
            Mssystem_1 ms1 = Mssystem_1.parse(ms);
            MsReturned rt = new BLLsystem_1().Savesystem_1(ms1);
            if (Convert.ToInt32(rt.strMsId) >= 0)
            {
                //注意不是追加 是先加
                FineUICore.PageContext.RegisterStartupScript("savecallback('" + rt.strMsId + "','" + type + "');");
                ShowNotify("保存成功");
            }
            return FineUICore.UIHelper.Result();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult lodeGrid(JArray fields)
        {
            InitData(fields);
            return FineUICore.UIHelper.Result();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult search_Click(JObject ms, JArray fields)
        {
            Mssystem_1 data = Mssystem_1.parse(ms);
            MsQuery msq = new MsQuery();
            if (!string.IsNullOrEmpty(data.System_1_20))
            {
                msq.strquery += " and System_1_20 like '%" + data.System_1_20 + "%'";
            }
            if (!string.IsNullOrEmpty(data.System_1_120))
            {
                msq.strquery += " and System_1_120 like '%" + data.System_1_120 + "%'";
            }
            if (!string.IsNullOrEmpty(data.System_1_140))
            {
                msq.strquery += " and System_1_140 like '%" + data.System_1_140 + "%'";
            }
            msq.strquery += " and 1=1 and system_1_20!='admin' ";
            DataTable dt = ((DataSet)new BLLsystem_1().Getsystem_1ByQuery(msq).data).Tables[0];
            if (FineUICore.UIHelper.IsFineUIAjaxPostBack())
            {
                var Grid1 = FineUICore.UIHelper.Grid("Grid1");
                if (fields != null)
                {
                    Grid1.DataSource(dt, fields);
                }
            }
            return FineUICore.UIHelper.Result();
        }


    }
}